﻿'use strict';

define([
    'baseClass',
], function (BaseClass) {
    var WebServiceProxy = BaseClass.extend({
        defaults: {
            contentType: "application/json",
            url: '',
            verb: 'POST',
        },

        objToHash: function (obj) {
            var hash = [];

            for (var prop in obj)
                hash.push({ Key: prop, Value: obj[prop] });

            return hash;
        },
        hashToObj: function (hash) {
            var obj;

            if (hash) {
                obj = {};

                _.each(hash, function(item, i) {
                    obj[item.Key] = item.Value;
                });
            }
            else {
                obj = null;
            }

            return obj;
        },
        hashArrayToObjArray: function (hashArray) {
            var objArray;

            if (hashArray) {
                var that = this;

                objArray = [];

                _.each(hashArray, function (item, i) {
                    objArray.push(that.hashToObj(item));
                });
            }
            else {
                objArray = null;
            }

            return objArray;
        },
        parseResponse: function (resp) {
            if ((resp) && (resp.Data))
                resp.Data = this.hashArrayToObjArray(resp.Data);

            return resp;
        },
        request: function (method, data, options) {
            options || (options = {});

            var that = this,
                fn_success = function (document, message, response) {
                    var respJson = response.responseJSON;

                    if (options.success)
                        options.success(respJson);

                    that.trigger('success', this, respJson, method)
                        .trigger(sprintf('success:%s', method), this, respJson, method)
                        .trigger('after', this, method)
                        .trigger(sprintf('after:%s', method), this, method);
                },
                fn_error = function (jqReq, errorMsg, status) {
                    if (options.error)
                        options.error(jqReq, errorMsg, status);

                    that.trigger('failed', this, jqReq, errorMsg, status, method)
                        .trigger(sprintf('failed:%s', method), this, jqReq, errorMsg, status, method)
                        .trigger('after', this, method)
                        .trigger(sprintf('after:%s', method), this, method);
                };

            this.trigger('before', this, method, data)
                .trigger(sprintf('before:%s', method), this, method, data);

            $.ajax(
                _.extend(
                    {},
                    this.attributes,
                    {
                        url: this.attributes.url + method,

                        data: data,
                        error: fn_error,
                        success: fn_success,
                    },
                    options
                )
            );

            return this;
        },
    });

    return WebServiceProxy;
});